import requests 
import base64
def system(image_path):
    """该函数为百度AI中美食识别的api调用【图片相对地址/绝对地址】"""
    # 获取access_token
    host = 'https://aip.baidubce.com/oauth/2.0/token?'

    payload = {
        'grant_type':'client_credentials',
        'client_id':'eREailrg19DA07HXwIQN4d60',
        'client_secret':'rxkzNhc0wCYftv6KaMOgafwrktTyVmEm'}
    
    response = requests.get(host,params=payload)
    request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/dish"
    access_token=response.json()['access_token']
    # 二进制方式打开图片文件
    f = open(image_path, 'rb')
    img = base64.b64encode(f.read())

    params = {"image":img,"top_num":5}
    access_token = access_token
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)

    result = ('识别一：'+response.json()['result'][0]['name']+' (置信度'+str(response.json()['result'][0]['probability'])[:4]+ '%)'+'\n'
    '识别二：'+response.json()['result'][1]['name']+' (置信度'+str(response.json()['result'][1]['probability'])[:4]+ '%)'+'\n'
    '识别三：'+response.json()['result'][2]['name']+' (置信度'+str(response.json()['result'][2]['probability'])[:4]+ '%)'+'\n'
    '识别四：'+response.json()['result'][3]['name']+' (置信度'+str(response.json()['result'][3]['probability'])[:4]+ '%)'+'\n'
    '识别五：'+response.json()['result'][4]['name']+' (置信度'+str(response.json()['result'][4]['probability'])[:4]+ '%)')
    return print(result)
